//
//  ShareModel.m
//  huashiedu
//
//  Created by oceans on 2018/9/10.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ShareModel.h"
#import <WXApi.h>

@interface ShareModel ()
@property (strong, nonatomic) NSDictionary *shareInfo;
@property (strong, nonatomic) UIView *coverView;
@property (strong, nonatomic) UIView *successView;
@property (strong, nonatomic) UILabel *titleLabel;
@end

static ShareModel *shared;
@implementation ShareModel

+ (instancetype)shared {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        shared = [[self alloc] init];
    });
    return shared;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        shared = [super allocWithZone:zone];
    });
    return shared;
}

- (id)copyWithZone:(NSZone *)zone {
    return shared;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)shareWithType:(int)type info:(NSDictionary *)info {
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    if (type == 0) {
        req.scene = WXSceneSession;
    }else {
        req.scene = WXSceneTimeline;
    }
    
    //创建分享内容对象
    WXMediaMessage *urlMessage = [WXMediaMessage message];
    urlMessage.title = [info objectForKey:@"title"];//分享标题
//    urlMessage.description = kLinkDescription;//分享描述
    [urlMessage setThumbImage:[UIImage imageNamed:@"loginicon"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
    
    //创建多媒体对象
    WXWebpageObject *webObj = [WXWebpageObject object];
    webObj.webpageUrl = [info objectForKey:@"link"];//分享链接
    
    //完成发送对象实例
    urlMessage.mediaObject = webObj;
    req.message = urlMessage;
    
    [WXApi sendReq:req];
}

- (void)showShareView:(NSDictionary *)share {
    
    _shareInfo = share;
    [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
    self.coverView.hidden = NO;
}

- (void)hideShareView {
    _coverView.hidden = YES;
    [_coverView removeFromSuperview];
}

- (void)buttonsPressed:(UIButton *)sender {
    
    if (sender.tag == 0) {
        [self shareWithType:0 info:_shareInfo];
    }else {
        [self shareWithType:1 info:_shareInfo];
    }
}

- (UIView *)coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _coverView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        
        UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 182, ScreenWidth, 182)];
        back.backgroundColor = [UIColor whiteColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideShareView)];
        [_coverView addGestureRecognizer:tap];
        
        UIButton *pengyou = [UIButton buttonWithType:UIButtonTypeCustom];
        pengyou.frame = CGRectMake(0, 0, 62, 88);
        [pengyou addTarget:self action:@selector(buttonsPressed:) forControlEvents:UIControlEventTouchUpInside];
        pengyou.center = CGPointMake(ScreenWidth * 0.35, 65);
        [pengyou setImage:[UIImage imageNamed:@"share_pengyou"] forState:UIControlStateNormal];
        [back addSubview:pengyou];
        
        UIButton *quan = [UIButton buttonWithType:UIButtonTypeCustom];
        quan.frame = CGRectMake(0, 0, 62, 88);
        [quan addTarget:self action:@selector(buttonsPressed:) forControlEvents:UIControlEventTouchUpInside];
        quan.center = CGPointMake(ScreenWidth * 0.65, 65);
        quan.tag = 1;
        [quan setImage:[UIImage imageNamed:@"share_quan"] forState:UIControlStateNormal];
        [back addSubview:quan];
        
        CALayer *line = [[CALayer alloc] init];
        line.frame = CGRectMake(15, 128, ScreenWidth - 30, 1);
        line.backgroundColor = [UIColor colorWithRed:228/255.0 green:228/255.0 blue:228/255.0 alpha:1.0].CGColor;
        [back.layer addSublayer:line];
        
        UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
        cancel.frame = CGRectMake(0, 129, ScreenWidth, 53);
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(hideShareView) forControlEvents:UIControlEventTouchUpInside];
        [cancel setTitleColor:COLOR(147, 147, 147, 1) forState:UIControlStateNormal];
        cancel.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [back addSubview:cancel];
        
        [_coverView addSubview:back];
    }
    return _coverView;
}

- (UIView *)successView {
    if (!_successView) {
        
        _successView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _successView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        
        UIView *back = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 30, 257)];
        back.center = CGPointMake(ScreenWidth*0.5, ScreenHeight*0.5);
        back.backgroundColor = [UIColor whiteColor];
        back.layer.cornerRadius = 3.0;
        back.layer.masksToBounds = YES;
        
        UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
        cancel.frame = CGRectMake(ScreenWidth - 30 - 40, 0, 40, 40);
        [cancel addTarget:self action:@selector(hideSuccessView) forControlEvents:UIControlEventTouchUpInside];
        cancel.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [cancel setImage:[UIImage imageNamed:@"chacha"] forState:UIControlStateNormal];
        [back addSubview:cancel];
        
        UIButton *confirm = [UIButton buttonWithType:UIButtonTypeCustom];
        confirm.frame = CGRectMake(30, 200, ScreenWidth - 90, 44);
        confirm.backgroundColor = COLOR(253, 119, 77, 1);
        [confirm addTarget:self
                    action:@selector(hideSuccessView) forControlEvents:UIControlEventTouchUpInside];
        confirm.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [confirm setTitle:@"确定" forState:UIControlStateNormal];
        [confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        confirm.layer.cornerRadius = 3.0;
        confirm.layer.masksToBounds = YES;
        [back addSubview:confirm];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 222, 50)];
        _titleLabel.center = CGPointMake(back.bounds.size.width * 0.5, 142);
        _titleLabel.numberOfLines = 2;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = COLOR(79, 79, 79, 1);
        [back addSubview:_titleLabel];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 147, 77)];
        imageView.image = [UIImage imageNamed:@"create_success"];
        imageView.center = CGPointMake(back.bounds.size.width * 0.5, 65);
        [back addSubview:imageView];
        
        [_successView addSubview:back];
    }
    return _successView;
}

- (void)hideSuccessView {
    _successView.hidden = YES;
    [_successView removeFromSuperview];
}

- (void)showCreateSuccessView:(NSString *)title {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.successView];
    self.successView.hidden = NO;
    self.titleLabel.text = title;
}

@end
